﻿#include "SmartControl.h"
#include <QScrollArea>
#include <QFileDialog>
#include <QSystemTrayIcon>
#include "UToolBarManager.h"
#include "Global.h"
#include "UBaseNode.h"
#include "UPointInfo.h"
#include "USettingsPage.h"
#include "CLoadFromConfig.h"
#include "UAlarm.h"
#include "CSpeechAlert.h"
#include <qdebug.h>
#include "CPublicData.h"
SmartControl::SmartControl(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
	CPublicData::instance().init();
	qRegisterMetaType<CBaseNode*>("CBaseNode*");
	initUi();
	QList<int> sizes;
	sizes << 700 << 200;
	ui.splitter->setSizes(sizes);
	resize(1600, 900);
}

SmartControl::~SmartControl()
{
	destroyUi();
}

void SmartControl::initUi()
{

	/**
	* @ Author: Gaojia
	* @ Description:  绑定菜单槽函数
	*/
	connect(ui.action_DevRun, &QAction::triggered, this, &SmartControl::slot_start);
	connect(ui.action_DevStop, &QAction::triggered, this, &SmartControl::slot_stop);
	connect(ui.action_New, &QAction::triggered, this, &SmartControl::slot_New);
	connect(ui.memu_Save, &QAction::triggered, this, &SmartControl::slot_Save);
	connect(ui.action_S, &QAction::triggered, this, &SmartControl::slot_SaveAs);
	connect(ui.memu_Open, &QAction::triggered, this, &SmartControl::slot_Open);
	connect(ui.action_Close, &QAction::triggered, this, &SmartControl::slot_Close);
	connect(ui.memu_AddNode, &QAction::triggered, this, &SmartControl::slot_AddNode);
	connect(ui.memu_AddDevice, &QAction::triggered, this, &SmartControl::slot_AddDevice);
	connect(ui.memu_AddPoint, &QAction::triggered, this, &SmartControl::slot_AddPoint);
	connect(ui.action_Cut, &QAction::triggered, this, &SmartControl::slot_Cut);
	connect(ui.action_Past, &QAction::triggered, this, &SmartControl::slot_Past);
	connect(ui.action_Undo, &QAction::triggered, this, &SmartControl::slot_Undo);
	connect(ui.action_Redo, &QAction::triggered, this, &SmartControl::slot_Redo);
	connect(ui.memu_Delete, &QAction::triggered, this, &SmartControl::slot_Delete);

	connect(ui.memu_AddNodeScene, &QAction::triggered, this, &SmartControl::slot_addScene);
	connect(ui.memu_AddDeviceScene, &QAction::triggered, this, &SmartControl::slot_addSceneNode);
	connect(ui.memu_DeleteScene, &QAction::triggered, this, &SmartControl::slot_deleteScene);
	connect(ui.memu_AddNodeCarouse, &QAction::triggered, this, &SmartControl::slot_addCarouseNode);
	connect(ui.memu_DeleteCarouse, &QAction::triggered, this, &SmartControl::slot_deleteCarouseNode);
	connect(ui.memu_Set, &QAction::triggered, this, &SmartControl::slot_setInterval);
	connect(ui.memu_RunCarouse, &QAction::triggered, this, &SmartControl::slot_runCarouse);
	connect(ui.memt_StopCarouse, &QAction::triggered, this, &SmartControl::slot_stopCarouse);

	ui.groupBox_6->installEventFilter(this);
	/**
	* @ Author: Gaojia
	* @ Description:  初始化公共工具栏
	*/
	ui.mainToolBar->setToolButtonStyle(Qt::ToolButtonIconOnly);
	ui.memu_Open->setIcon(QIcon(":/SmartControl/Resource/toolbar/open.png"));
	ui.memu_Save->setIcon(QIcon(":/SmartControl/Resource/toolbar/save.png"));
	ui.tool_Open->setToolTip(A2T("打开工程")); // 鼠标悬停显示提示	
	ui.tool_Save->setToolTip(A2T("保存工程")); // 鼠标悬停显示提示
	g_pToolBarMang = new UToolBarManager(ui.mainToolBar,ui.menuBar, this);
	connect(ui.tool_Save, &QAction::triggered, this, &SmartControl::slot_Save);

	/**
	* @ Author: Gaojia
	* @ Description:  初始化图标
	*/
	QSystemTrayIcon* mainIcon = new QSystemTrayIcon(this);
	mainIcon->setToolTip(A2T("科远集控系统平台"));
	mainIcon->setIcon(QIcon(":/SmartControl/Resource/rob.png"));
	QMenu* mainMenu = new QMenu((QWidget*)QApplication::desktop());
	QAction* quitAction = new QAction(this);
	quitAction->setText(A2T("退出"));
	mainMenu->addAction(quitAction);
	connect(quitAction, &QAction::triggered, this, &SmartControl::close);
	mainIcon->setContextMenu(mainMenu);
	mainIcon->show();
	this->setWindowIcon(QIcon(":/SmartControl/Resource/rob.png"));

	/**
	* @ Author: Gaojia
	* @ Description:  初始化设备和场景界面
	*/
	CLoadFromConfig::loadFromConfig();
	ui.tab_Device->initUi();
	ui.tab_Scene->initUi();
	ui.tab_Device->refushUi();
	ui.tab_Scene->refushUi();
	/**
	* @ Author: Gaojia
	* @ Description:  创建日志控件
	*/


	ui.verticalLayout->addWidget(CLogOper::instance().createLogWidget(A2T("系统")));

	SExecLog sLog;
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("设备节点加载成功");
	sLog.m_sDevName = A2T("系统");
	CLogOper::instance().addMsg(sLog);
	sLog.m_sDescriptionl = A2T("场景和轮播加载成功");
	sLog.m_sDevName = A2T("系统");
	CLogOper::instance().addMsg(sLog);
	/**
	* @ Author: Gaojia
	* @ Description: 把设备节点传给场景
	*/
	ui.tab_Scene->setRootNode(g_pRoot);
	connect(ui.tabWidget, &QTabWidget::currentChanged,this, &SmartControl::onTabChanged);

	ui.splitter->widget(0)->setMinimumHeight(40); // 左边最小 200
	ui.splitter->widget(1)->setMinimumHeight(40); // 右边最小 300
	ui.splitter->setCollapsible(0, false);
	ui.splitter->setCollapsible(1, false);

	CSpeechAlert::getInstance();
}


void SmartControl::slot_start()
{
	g_pRoot->start();
	SExecLog sLog;
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("所有设备开始运行");
	sLog.m_sDevName = A2T("系统");
	CLogOper::instance().addMsg( sLog);
}

void SmartControl::slot_stop()
{
	g_pRoot->stop();
	SExecLog sLog;
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("所有设备停止运行");
	sLog.m_sDevName = A2T("系统");
	CLogOper::instance().addMsg(sLog);
}

void SmartControl::slot_New()
{
	QString sStr = showGetText(this, A2T("新建项目"), A2T("请输入项目名称"), "");
	if (sStr != "")
	{
		destroyUi();
		CLoadFromConfig::createConfig(sStr);
		load();
	}
}

void SmartControl::slot_Save()
{
	CLoadFromConfig::saveToConfig();
	ui.statusBar->showMessage(A2T("保存配置文件成功"));
}

void SmartControl::slot_Open()
{
	QString defaultPath = QCoreApplication::applicationDirPath();  // 获取 exe 同级目录

	QString fileName = QFileDialog::getOpenFileName(this,
		A2T("选择 XML 文件"),
		defaultPath,
		A2T("XML 文件 (*.xml);;所有文件 (*.*)"));

	if (!fileName.isEmpty()) 
	{
		destroyUi();
		CLoadFromConfig::loadFromConfig(fileName);
		load();
		ui.statusBar->showMessage(A2T("导入文件") + fileName + A2T("成功"));
	}
}

void SmartControl::slot_SaveAs()
{
	CLoadFromConfig::saveAs();
}

void SmartControl::slot_AddNode()
{
	ui.tab_Device->m_pNodeManager->slot_addNode();
	ui.statusBar->showMessage(A2T("添加节点成功"));
}

void SmartControl::slot_AddDevice()
{
	ui.tab_Device->m_pNodeManager->slot_addPage();
	ui.statusBar->showMessage(A2T("添加设备成功"));
}

void SmartControl::slot_AddPoint()
{
	ui.tab_Device->m_pNodeManager->slot_addPoint();
	ui.statusBar->showMessage(A2T("添加测点成功"));
}

void SmartControl::slot_Cut()
{
	//ui.widget->copyNode(m_pCurrNode);
	ui.statusBar->showMessage(A2T("剪切当前节点"));
}

void SmartControl::slot_Past()
{
	//ui.widget->pasteNodes(m_pCurrNode);
	ui.statusBar->showMessage(A2T("粘贴到当前节点下"));
}

void SmartControl::slot_Undo()
{
	//ui.widget->undo();
}

void SmartControl::slot_Redo()
{
	//ui.widget->redo();
}

void SmartControl::slot_Delete()
{
	ui.tab_Device->m_pNodeManager->slot_deleteNode();
}


void SmartControl::onTabChanged(int nIndex)
{
	if (nIndex == 0)
	{
		g_pToolBarMang->setToolBtn(UToolBarManager::Device, 1);
	}
	else
	{
		ui.tab_Scene->refushCombox();
		g_pToolBarMang->setToolBtn(UToolBarManager::Carouse, 1);
	}
	
}

void SmartControl::slot_addScene()
{
	ui.tab_Scene->m_pSceneManager->slot_addNode();
}

void SmartControl::slot_addSceneNode()
{
	ui.tab_Scene->m_pSceneManager->slot_addPage();
}

void SmartControl::slot_deleteScene()
{
	ui.tab_Scene->m_pSceneManager->slot_deleteNode();
}

void SmartControl::slot_addCarouseNode()
{
	ui.tab_Scene->m_pCarouselManager->slot_addNode();
}

void SmartControl::slot_deleteCarouseNode()
{
	ui.tab_Scene->m_pCarouselManager->slot_deleteNode();
}

void SmartControl::slot_runCarouse()
{
	ui.tab_Scene->m_pCarouselManager->slot_runCarouse();
}

void SmartControl::slot_stopCarouse()
{
	ui.tab_Scene->m_pCarouselManager->slot_stopCarouse();
}

void SmartControl::slot_setInterval()
{
	ui.tab_Scene->m_pCarouselManager->slot_setInterval();
}

void SmartControl::slot_enlarge()
{
	ui.tab_Scene->m_pCarouselManager->slot_enlarge();
}

void SmartControl::slot_Close()
{
	close();
}



// 在你的类里
bool SmartControl::eventFilter(QObject* obj, QEvent* event)
{
	if (event->type() == QEvent::MouseButtonRelease)
	{
		if (obj == ui.groupBox_6)
		{
			QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
			if (mouseEvent->button() == Qt::LeftButton)
			{
				int nIndex = ui.tabWidget->currentIndex();
				if (nIndex == 0)
				{
					g_pToolBarMang->setToolBtn(UToolBarManager::Device, 2);
				}
				else
				{
					ui.tab_Scene->clickLog();
				}
			}
		}
	}
	return QWidget::eventFilter(obj, event);
}


//释放资源
void SmartControl::destroyUi()
{
	ui.tab_Device->destroyUi();   //释放表格资源
	g_mapCardWidget.clear();
	//UAlarm::stopRufush();        //光字牌报警停止刷新
	if (g_pRoot != nullptr)
	{
		delete g_pRoot->getItem();
		delete g_pRoot;
		g_pRoot = nullptr;
	}
	if (g_pRootScene != nullptr)
	{
		delete g_pRootScene->getItem();
		delete g_pRootScene;
		g_pRootScene = nullptr;
	}
	if (g_pRootCarouse != nullptr)
	{
		delete g_pRootCarouse->getItem();
		delete g_pRootCarouse;
		g_pRootCarouse = nullptr;
	}
}

void SmartControl::load()
{
	ui.tab_Device->refushUi();
	ui.tab_Scene->refushUi();
}

void SmartControl::closeEvent(QCloseEvent *event)
{
	event->ignore();   //屏蔽关闭事件
	int nRect =QMessageBox::question(nullptr, A2T("退出"), A2T("是否确定关闭程序"), QMessageBox::Yes | QMessageBox::No | QMessageBox::Close);
	//确认关闭
	if (QMessageBox::Yes == nRect)
	{
		//退出程序
		event->accept();

		QCoreApplication::quit();
	}
}
